 using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;

public class ProindexinfoUpdateHandler : IRequestHandler<ProindexinfoUpdateRequest, ApiResult<Proindexinfo>>
{
    private readonly IBaseRepository<Proindexinfo> _repository;
    public ProindexinfoUpdateHandler(IBaseRepository<Proindexinfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<Proindexinfo>> Handle(ProindexinfoUpdateRequest request, CancellationToken cancellationToken)
    {
        var entity = await _repository.GetByIdAsync(request.Id);
        if (entity == null)
            return ApiResult<Proindexinfo>.Fail("未找到要修改的数据");

        entity.ProductNameId = request.ProductNameId;
        entity.BatchNo = request.BatchNo;
        entity.ShelfLife = request.ShelfLife;
        entity.LicenseNo = request.LicenseNo;
        entity.Manufacturer = request.Manufacturer;
        entity.CoverImage = request.CoverImage;
        entity.InfoCompleteness = request.InfoCompleteness;
        entity.IsDeleted = request.IsDeleted;

        try
        {
            await _repository.UpdateAsync(entity);
            return ApiResult<Proindexinfo>.Success(entity, "修改成功");
        }
        catch (System.Exception ex)
        {
            return ApiResult<Proindexinfo>.Fail($"修改失败: {ex.Message}");
        }
    }
}